package com.sy.simpleegg.util

import com.sy.simpleegg.ext.logFile
import java.io.File

object UsbUtil {
    fun getUsbPaths(): MutableList<String>{
        val usbPaths = mutableListOf<String>()
        try {
            val storageDir = File("/storage")
            val files = storageDir.listFiles()
            files?.let {
                for (i in it) {
                    if (i.isDirectory && isUsbDevice(i)) {
                        usbPaths.add(i.absolutePath)
                    }
                }
            }
        }catch (e: Exception){
            e.printStackTrace()
            logFile("获取USB路径异常：${e.message}")
        }
        return usbPaths
    }

    private fun isUsbDevice(file: File): Boolean{
        try {
            val files = file.listFiles()
            files?.let {
                for (i in it){
//                    if (i.name.startsWith("sd") || i.name.startsWith("udisk")){
//                        return true
//                    }
                    if (!i.name.startsWith("self") && !i.name.startsWith("emulated")){
                        return true
                    }
                }
            }
        }catch (e: Exception){
            e.printStackTrace()
            logFile("判断USB路径异常：${e.message}")
        }
        return false
    }
}